package cn.crdeng.frametest.generator;

import cn.crdeng.frametest.subject.domain.BaseTime;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine;
import com.baomidou.mybatisplus.generator.fill.Column;
import com.baomidou.mybatisplus.generator.fill.Property;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class MybatisPlusGenerator {
    private static final String url = "jdbc:mysql://182.61.1.45:3306/frametest?useUnicode=true&characterEncoding=utf-8&useSSL=false&allowMultiQueries=true&serverTimezone=Hongkong&allowPublicKeyRetrieval=true";
    private static final String username = "root";
    private static final String password = "mei123456";
    private static final String moduleName = "subject"; //模块名称


    public static void main(String[] args) {
        List<String> tables = new ArrayList<>();
        tables.add("student");
        //调用构造器
        generator(tables);
    }

    public static void generator(List<String> tables) {
        FastAutoGenerator.create(url, username, password).globalConfig(builder -> {
                    builder.fileOverride()
                            .author("crdeng")
                            .outputDir(System.getProperty("user.dir") + "\\src\\main\\java")
                            .enableSwagger()
                            .disableOpenDir()
                            .commentDate("yyyy-MM-dd");

                }).packageConfig(builder -> {
                    builder.parent("cn.crdeng.frametest")
                            .moduleName("subject")
                            .entity("domain")
                            .service("service")
                            .serviceImpl("service.Impl")
                            .mapper("mapper")
                            .controller("controller")
                            .xml("mapper")
                            .pathInfo(Collections.singletonMap(OutputFile.xml, System.getProperty("user.dir") + "\\src\\main\\resources\\mapper\\" + moduleName));


                }).strategyConfig(builder -> {
                    builder.addInclude(tables)
                            .entityBuilder()
                            .enableLombok() //开启Lombok
                            .logicDeleteColumnName("flag")//逻辑删除字段
                            .disableSerialVersionUID()
                            .superClass(BaseTime.class)
                            .addSuperEntityColumns( "create_time", "create_by", "update_time", "update_by")
                            .addTableFills(new Column("create_time", FieldFill.INSERT),new Column("create_by", FieldFill.INSERT))
                            .addTableFills(new Column("update_time", FieldFill.INSERT_UPDATE),new Column("update_by", FieldFill.INSERT_UPDATE))
                            .mapperBuilder()
                            .enableMapperAnnotation()//@mapper 开启
                            .controllerBuilder()
                            .enableRestStyle()
                           ;

                }).templateEngine(new VelocityTemplateEngine())
                .execute();

    }

}
